<?php
namespace app\facade;

use think\Facade;

class Token extends Facade {
    public static function create($uid) {
        $data = [
            'time'  => time(),
            'uid'   => $uid,
            'msectime'  => self::msectime(),
        ];
        $json = json_encode($data);
        return Str::code($json,'E',config('key.token'));
    }

    private static function msectime() {
        list($msec, $sec) = explode(' ', microtime());
        $msectime = (float)sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000);        
        return $msectime;
    }

    public static function getUid($token = 'null') {
        if ($token == 'null') {
            $token = cookie('token');
        }
        $json = Str::code($token,'D',config('key.token'));
        $data = json_decode($json);
        return $data->uid;
    }
}